import { reqGetNew ,reqGetSeckill,reqGetgroupon,reqGetcountdown,reqGetindexgoods,reqGetcoupon} from "../../request/api";
//初始值
const initState = {
  //初始化数据
  //新人专享
  getnew:[],
  //秒杀
  getseckill:[],
  //万人团
  getgroupon:[],
  //倒计时
  getcountdown:[],
  //商品信息首页
  indexgoods:[],
  //导航标识
  flag:1,
  //优惠卷
  getcoupon:[]
};

//actionTypes
const types = {
  //修改数据 type
  CHANGE_GETNEW: "HOME_CHANGE_GETNEW",
  CHANGE_GETSECKILL: "HOME_CHANGE_GETSECKILL",
  CHANGE_GETGROUPON: "HOME_CHANGE_GETGROUPON",
  CHANGE_GETCOUNTDOWN: "HOME_CHANGE_GETCOUNTDOWN",
  CHANGE_GETINDEXGOODS: "HOME_CHANGE_GETINDEXGOODS",
  CHANGE_FLAG: "HOME_CHANGE_FLAG",
  CHANGE_GETCOUPON: "HOME_CHANGE_GETCOUPON",
};

//reducer 修改数据
const reducer = (state = initState, action) => {
  switch (action.type) {
    // 新人专享
    case types.CHANGE_GETNEW:
      return {
        ...state,
        getnew: action.getnew,
      };
    //秒杀
    case types.CHANGE_GETSECKILL:
        return{
            ...state,
            getseckill:action.getseckill,
        };
    //万人团
    case types.CHANGE_GETGROUPON:
        return{
            ...state,
            getgroupon:action.getgroupon,
        };
    //倒计时
    case types.CHANGE_GETCOUNTDOWN:
        return{
            ...state,
            getcountdown:action.getcountdown,
        };
      ////商品信息首页
    case types.CHANGE_GETINDEXGOODS:
        return{
             ...state,
            getindexgoods:action.getindexgoods,
        };
    case types.CHANGE_FLAG:
        return{
             ...state,
            flag:action.flag,
        };  
      // 优惠卷 
    case types.CHANGE_GETCOUPON:
        return{
            ...state,
            getcoupon:action.getcoupon,
        };
    default:
      return state;
  }
};
//actionCreators组件派发的动作
export const actions = {
  //新人专享
  changeGetnew: (getnew) => ({ type: types.CHANGE_GETNEW, getnew}),
  //秒杀
  changeGetseckill: (getseckill) => ({ type: types.CHANGE_GETSECKILL, getseckill}),
  //万人团
  changeGetgroupon: (getgroupon) => ({ type: types.CHANGE_GETGROUPON, getgroupon}),
   //倒计时
   changeGetcountdown: (getcountdown) => ({ type: types.CHANGE_GETCOUNTDOWN, getcountdown}),
   //商品信息首页
   changeGetindexgoods: (getindexgoods) => ({ type: types.CHANGE_GETINDEXGOODS, getindexgoods}),
   //导航标识
   changeFlag: (flag) => ({ type: types.CHANGE_FLAG, flag}),
   //优惠券
   changeGetcoupon: (getcoupon) => ({ type: types.CHANGE_GETCOUPON, getcoupon}),

  //请求 异步操作
  //新人专享
  reqGetNew: (history) => (dispatch, getState) => {
    reqGetNew().then((res) => {
        if(res.data.code===200){
             //存信息
          dispatch(actions.changeGetnew(res.data.list))
        }
    });
  },
  //秒杀
  reqGetSeckill: (history) => (dispatch, getState) => {
    reqGetSeckill().then((res) => {
        if(res.data.code===200){
             //存信息
          dispatch(actions.changeGetseckill(res.data.list))
        }
    });
  },
  //万人团
  reqGetgroupon: (history) => (dispatch, getState) => {
    reqGetgroupon().then((res) => {
        if(res.data.code===200){
             //存信息
          dispatch(actions.changeGetgroupon(res.data.list))
        }
    });
  },
  //倒计时
  reqGetcountdown: (history) => (dispatch, getState) => {
    reqGetcountdown().then((res) => {
        if(res.data.code===200){
             //存信息
          dispatch(actions.changeGetcountdown(res.data.list))
        }
    });
  },
   //商品信息首页
   reqGetindexgoods: (history) => (dispatch, getState) => {
    reqGetindexgoods().then((res) => {
        if(res.data.code===200){
             //存信息
          dispatch(actions.changeGetindexgoods(res.data.list))
        }
    });
  },
   //优惠券
   reqGetcoupon: (history) => (dispatch, getState) => {
    reqGetcoupon().then((res) => {
        if(res.data.code===200){
             //存信息
          dispatch(actions.changeGetcoupon(res.data.list))
        }
    });
  },
};

//selectors导出数据
export const getGetnew = state => state.home.getnew;
export const getGetseckill = state => state.home.getseckill;
export const getGetgroupon = state => state.home.getgroupon;
export const getGetcountdown = state => state.home.getcountdown;
export const getGetindexgoods= state => state.home.getindexgoods;
export const getFlag= state => state.home.flag;
export const getGetcoupon= state => state.home.getcoupon;

export default reducer;
